<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>digichef.tagging.generic</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="digichef-module.html">Package&nbsp;digichef</a> ::
        <a href="digichef.tagging-module.html">Package&nbsp;tagging</a> ::
        Module&nbsp;generic
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="digichef.tagging.generic-pysrc.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<h1 class="epydoc">Source Code for <a href="digichef.tagging.generic-module.html">Module digichef.tagging.generic</a></h1>
<pre class="py-src">
<a name="L1"></a><tt class="py-lineno"> 1</tt>  <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">django</tt><tt class="py-op">.</tt><tt class="py-name">contrib</tt><tt class="py-op">.</tt><tt class="py-name">contenttypes</tt><tt class="py-op">.</tt><tt id="link-0" class="py-name" targets="Module digichef.core.models=digichef.core.models-module.html,Module digichef.profiles.models=digichef.profiles.models-module.html,Module digichef.recipes.models=digichef.recipes.models-module.html,Module digichef.recommender.models=digichef.recommender.models-module.html,Module digichef.registration.models=digichef.registration.models-module.html,Module digichef.tagging.models=digichef.tagging.models-module.html,Module digichef.tagging.tests.models=digichef.tagging.tests.models-module.html,Module digichef.util.models=digichef.util.models-module.html,Module digichef.voting.models=digichef.voting.models-module.html,Module digichef.voting.tests.models=digichef.voting.tests.models-module.html"><a title="digichef.core.models
digichef.profiles.models
digichef.recipes.models
digichef.recommender.models
digichef.registration.models
digichef.tagging.models
digichef.tagging.tests.models
digichef.util.models
digichef.voting.models
digichef.voting.tests.models" class="py-name" href="#" onclick="return doclink('link-0', 'models', 'link-0');">models</a></tt> <tt class="py-keyword">import</tt> <tt class="py-name">ContentType</tt> </tt>
<a name="L2"></a><tt class="py-lineno"> 2</tt>  <tt class="py-line"> </tt>
<a name="fetch_content_objects"></a><div id="fetch_content_objects-def"><a name="L3"></a><tt class="py-lineno"> 3</tt> <a class="py-toggle" href="#" id="fetch_content_objects-toggle" onclick="return toggle('fetch_content_objects');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="digichef.tagging.generic-module.html#fetch_content_objects">fetch_content_objects</a><tt class="py-op">(</tt><tt class="py-param">tagged_items</tt><tt class="py-op">,</tt> <tt class="py-param">select_related_for</tt><tt class="py-op">=</tt><tt class="py-name">None</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="fetch_content_objects-collapsed" style="display:none;" pad="++" indent="++++"></div><div id="fetch_content_objects-expanded"><a name="L4"></a><tt class="py-lineno"> 4</tt>  <tt class="py-line">    <tt class="py-docstring">"""</tt> </tt>
<a name="L5"></a><tt class="py-lineno"> 5</tt>  <tt class="py-line"><tt class="py-docstring">    Retrieves ``ContentType`` and content objects for the given list of</tt> </tt>
<a name="L6"></a><tt class="py-lineno"> 6</tt>  <tt class="py-line"><tt class="py-docstring">    ``TaggedItems``, grouping the retrieval of content objects by model</tt> </tt>
<a name="L7"></a><tt class="py-lineno"> 7</tt>  <tt class="py-line"><tt class="py-docstring">    type to reduce the number of queries executed.</tt> </tt>
<a name="L8"></a><tt class="py-lineno"> 8</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L9"></a><tt class="py-lineno"> 9</tt>  <tt class="py-line"><tt class="py-docstring">    This results in ``number_of_content_types + 1`` queries rather than</tt> </tt>
<a name="L10"></a><tt class="py-lineno">10</tt>  <tt class="py-line"><tt class="py-docstring">    the ``number_of_tagged_items * 2`` queries you'd get by iterating</tt> </tt>
<a name="L11"></a><tt class="py-lineno">11</tt>  <tt class="py-line"><tt class="py-docstring">    over the list and accessing each item's ``object`` attribute.</tt> </tt>
<a name="L12"></a><tt class="py-lineno">12</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L13"></a><tt class="py-lineno">13</tt>  <tt class="py-line"><tt class="py-docstring">    A ``select_related_for`` argument can be used to specify a list of</tt> </tt>
<a name="L14"></a><tt class="py-lineno">14</tt>  <tt class="py-line"><tt class="py-docstring">    of model names (corresponding to the ``model`` field of a</tt> </tt>
<a name="L15"></a><tt class="py-lineno">15</tt>  <tt class="py-line"><tt class="py-docstring">    ``ContentType``) for which ``select_related`` should be used when</tt> </tt>
<a name="L16"></a><tt class="py-lineno">16</tt>  <tt class="py-line"><tt class="py-docstring">    retrieving model instances.</tt> </tt>
<a name="L17"></a><tt class="py-lineno">17</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="L18"></a><tt class="py-lineno">18</tt>  <tt class="py-line">    <tt class="py-keyword">if</tt> <tt class="py-name">select_related_for</tt> <tt class="py-keyword">is</tt> <tt class="py-name">None</tt><tt class="py-op">:</tt> <tt class="py-name">select_related_for</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt> </tt>
<a name="L19"></a><tt class="py-lineno">19</tt>  <tt class="py-line"> </tt>
<a name="L20"></a><tt class="py-lineno">20</tt>  <tt class="py-line">    <tt class="py-comment"># Group content object pks by their content type pks</tt> </tt>
<a name="L21"></a><tt class="py-lineno">21</tt>  <tt class="py-line">    <tt id="link-1" class="py-name" targets="Variable digichef.recommender.models.Recommender.objects=digichef.recommender.models.Recommender-class.html#objects,Variable digichef.registration.models.RegistrationProfile.objects=digichef.registration.models.RegistrationProfile-class.html#objects,Variable digichef.tagging.models.Tag.objects=digichef.tagging.models.Tag-class.html#objects,Variable digichef.tagging.models.TaggedItem.objects=digichef.tagging.models.TaggedItem-class.html#objects,Variable digichef.voting.models.Vote.objects=digichef.voting.models.Vote-class.html#objects"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-1', 'objects', 'link-1');">objects</a></tt> <tt class="py-op">=</tt> <tt class="py-op">{</tt><tt class="py-op">}</tt> </tt>
<a name="L22"></a><tt class="py-lineno">22</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">item</tt> <tt class="py-keyword">in</tt> <tt class="py-name">tagged_items</tt><tt class="py-op">:</tt> </tt>
<a name="L23"></a><tt class="py-lineno">23</tt>  <tt class="py-line">        <tt id="link-2" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-2', 'objects', 'link-1');">objects</a></tt><tt class="py-op">.</tt><tt class="py-name">setdefault</tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">.</tt><tt class="py-name">content_type_id</tt><tt class="py-op">,</tt> <tt class="py-op">[</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">.</tt><tt class="py-name">append</tt><tt class="py-op">(</tt><tt class="py-name">item</tt><tt class="py-op">.</tt><tt id="link-3" class="py-name" targets="Variable digichef.tagging.models.TaggedItem.object_id=digichef.tagging.models.TaggedItem-class.html#object_id,Variable digichef.voting.models.Vote.object_id=digichef.voting.models.Vote-class.html#object_id"><a title="digichef.tagging.models.TaggedItem.object_id
digichef.voting.models.Vote.object_id" class="py-name" href="#" onclick="return doclink('link-3', 'object_id', 'link-3');">object_id</a></tt><tt class="py-op">)</tt> </tt>
<a name="L24"></a><tt class="py-lineno">24</tt>  <tt class="py-line"> </tt>
<a name="L25"></a><tt class="py-lineno">25</tt>  <tt class="py-line">    <tt class="py-comment"># Retrieve content types and content objects in bulk</tt> </tt>
<a name="L26"></a><tt class="py-lineno">26</tt>  <tt class="py-line">    <tt class="py-name">content_types</tt> <tt class="py-op">=</tt> <tt class="py-name">ContentType</tt><tt class="py-op">.</tt><tt class="py-name">_default_manager</tt><tt class="py-op">.</tt><tt class="py-name">in_bulk</tt><tt class="py-op">(</tt><tt id="link-4" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-4', 'objects', 'link-1');">objects</a></tt><tt class="py-op">.</tt><tt class="py-name">keys</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">)</tt> </tt>
<a name="L27"></a><tt class="py-lineno">27</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">content_type_pk</tt><tt class="py-op">,</tt> <tt class="py-name">object_pks</tt> <tt class="py-keyword">in</tt> <tt id="link-5" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-5', 'objects', 'link-1');">objects</a></tt><tt class="py-op">.</tt><tt class="py-name">iteritems</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L28"></a><tt class="py-lineno">28</tt>  <tt class="py-line">        <tt id="link-6" class="py-name" targets="Variable digichef.profiles.forms.ProfileForm.Meta.model=digichef.profiles.forms.ProfileForm.Meta-class.html#model,Variable digichef.tagging.forms.TagAdminForm.Meta.model=digichef.tagging.forms.TagAdminForm.Meta-class.html#model"><a title="digichef.profiles.forms.ProfileForm.Meta.model
digichef.tagging.forms.TagAdminForm.Meta.model" class="py-name" href="#" onclick="return doclink('link-6', 'model', 'link-6');">model</a></tt> <tt class="py-op">=</tt> <tt class="py-name">content_types</tt><tt class="py-op">[</tt><tt class="py-name">content_type_pk</tt><tt class="py-op">]</tt><tt class="py-op">.</tt><tt class="py-name">model_class</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L29"></a><tt class="py-lineno">29</tt>  <tt class="py-line">        <tt class="py-keyword">if</tt> <tt class="py-name">content_types</tt><tt class="py-op">[</tt><tt class="py-name">content_type_pk</tt><tt class="py-op">]</tt><tt class="py-op">.</tt><tt id="link-7" class="py-name"><a title="digichef.profiles.forms.ProfileForm.Meta.model
digichef.tagging.forms.TagAdminForm.Meta.model" class="py-name" href="#" onclick="return doclink('link-7', 'model', 'link-6');">model</a></tt> <tt class="py-keyword">in</tt> <tt class="py-name">select_related_for</tt><tt class="py-op">:</tt> </tt>
<a name="L30"></a><tt class="py-lineno">30</tt>  <tt class="py-line">            <tt id="link-8" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-8', 'objects', 'link-1');">objects</a></tt><tt class="py-op">[</tt><tt class="py-name">content_type_pk</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt id="link-9" class="py-name"><a title="digichef.profiles.forms.ProfileForm.Meta.model
digichef.tagging.forms.TagAdminForm.Meta.model" class="py-name" href="#" onclick="return doclink('link-9', 'model', 'link-6');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">_default_manager</tt><tt class="py-op">.</tt><tt class="py-name">select_related</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">.</tt><tt class="py-name">in_bulk</tt><tt class="py-op">(</tt><tt class="py-name">object_pks</tt><tt class="py-op">)</tt> </tt>
<a name="L31"></a><tt class="py-lineno">31</tt>  <tt class="py-line">        <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L32"></a><tt class="py-lineno">32</tt>  <tt class="py-line">            <tt id="link-10" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-10', 'objects', 'link-1');">objects</a></tt><tt class="py-op">[</tt><tt class="py-name">content_type_pk</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt id="link-11" class="py-name"><a title="digichef.profiles.forms.ProfileForm.Meta.model
digichef.tagging.forms.TagAdminForm.Meta.model" class="py-name" href="#" onclick="return doclink('link-11', 'model', 'link-6');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">_default_manager</tt><tt class="py-op">.</tt><tt class="py-name">in_bulk</tt><tt class="py-op">(</tt><tt class="py-name">object_pks</tt><tt class="py-op">)</tt> </tt>
<a name="L33"></a><tt class="py-lineno">33</tt>  <tt class="py-line"> </tt>
<a name="L34"></a><tt class="py-lineno">34</tt>  <tt class="py-line">    <tt class="py-comment"># Set content types and content objects in the appropriate cache</tt> </tt>
<a name="L35"></a><tt class="py-lineno">35</tt>  <tt class="py-line">    <tt class="py-comment"># attributes, so accessing the 'content_type' and 'object'</tt> </tt>
<a name="L36"></a><tt class="py-lineno">36</tt>  <tt class="py-line">    <tt class="py-comment"># attributes on each tagged item won't result in further database</tt> </tt>
<a name="L37"></a><tt class="py-lineno">37</tt>  <tt class="py-line">    <tt class="py-comment"># hits.</tt> </tt>
<a name="L38"></a><tt class="py-lineno">38</tt>  <tt class="py-line">    <tt class="py-keyword">for</tt> <tt class="py-name">item</tt> <tt class="py-keyword">in</tt> <tt class="py-name">tagged_items</tt><tt class="py-op">:</tt> </tt>
<a name="L39"></a><tt class="py-lineno">39</tt>  <tt class="py-line">        <tt class="py-name">item</tt><tt class="py-op">.</tt><tt class="py-name">_object_cache</tt> <tt class="py-op">=</tt> <tt id="link-12" class="py-name"><a title="digichef.recommender.models.Recommender.objects
digichef.registration.models.RegistrationProfile.objects
digichef.tagging.models.Tag.objects
digichef.tagging.models.TaggedItem.objects
digichef.voting.models.Vote.objects" class="py-name" href="#" onclick="return doclink('link-12', 'objects', 'link-1');">objects</a></tt><tt class="py-op">[</tt><tt class="py-name">item</tt><tt class="py-op">.</tt><tt class="py-name">content_type_id</tt><tt class="py-op">]</tt><tt class="py-op">[</tt><tt class="py-name">item</tt><tt class="py-op">.</tt><tt id="link-13" class="py-name"><a title="digichef.tagging.models.TaggedItem.object_id
digichef.voting.models.Vote.object_id" class="py-name" href="#" onclick="return doclink('link-13', 'object_id', 'link-3');">object_id</a></tt><tt class="py-op">]</tt> </tt>
<a name="L40"></a><tt class="py-lineno">40</tt>  <tt class="py-line">        <tt class="py-name">item</tt><tt class="py-op">.</tt><tt class="py-name">_content_type_cache</tt> <tt class="py-op">=</tt> <tt class="py-name">content_types</tt><tt class="py-op">[</tt><tt class="py-name">item</tt><tt class="py-op">.</tt><tt class="py-name">content_type_id</tt><tt class="py-op">]</tt> </tt>
</div><a name="L41"></a><tt class="py-lineno">41</tt>  <tt class="py-line"> </tt><script type="text/javascript">
<!--
expandto(location.href);
// -->
</script>
</pre>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Apr  1 02:28:32 2010
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
